package com.frame.platform.dept;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import com.frame.common.SystemConfig;
import com.frame.database.DataBase;
import com.frame.hibernate.HibernateUtil;
import com.frame.pageform.component.tree.*;
import com.frame.platform.user.Wfuserdept;

public class DeptTreeJson extends TreeJson{

	private String[] deptId;
	private String userId;
	private boolean selectDept;
	private Session session;
	private boolean showId;
	private String expand;
	private DataBase dataBase=DataBase.getInstance();

	public Object getRoot() throws Exception {
		// TODO Auto-generated method stub
		DataBase dataBase=DataBase.getInstance();
		Wfdept dept=(Wfdept)dataBase.getBean(Wfdept.class, new Integer(1));
		if(dept==null){
			dept=new Wfdept();
			dept.setDeptid(new Integer(1));
			dept.setDeptname("ƽ̨ڜm");
			dept.setDeletetag(new Integer(0));
			dept.setPardeptid(0);
			dataBase.save(dept);
		}
		return dept;
	}

	public List query() throws Exception {
		// TODO Auto-generated method stub
		List trees=null;
		if(isLazy()){
			if(getNodeId()==null)
				setNodeId("0");
			trees=getSubDept(getNodeId());
			for(int i=0;i<trees.size();i++){
				TreeNode node=(TreeNode)trees.get(i);
				if(getExpand()!=null && getExpand().equals("true")){
					node.setExpand(true);
					List<TreeNode> clist=getSubDept(node.getId());
					node.setChildList(clist);
				}
			}
			return trees;
		}else{
			trees=new ArrayList();
			session = HibernateUtil.currentSession();
			Wfdept dept=(Wfdept)session.load(Wfdept.class, new Integer(getNodeId()));
			TreeNode node = new TreeNode(dept.getDeptid().toString(),
					dept.getPardeptid() == null ? "" : dept.getPardeptid()
							.toString(), dept.getDeptname());
			node.setExpand(true);
			node.setIcon(SystemConfig.getInstance().getContextPath()+"/platform/images/dept.png");
			if(selectDept){
				getSelectDepts();
				for(int i=0;i<deptId.length;i++){
					if(deptId[i].equals(dept.getDeptid().toString())){
						node.setChecked(true);
						break;
					}
				}
			}
			getAllChildren(node, dept);
			trees.add(node);
			return trees;
		}
	}
	private List<TreeNode> getSubDept(String pid) throws NumberFormatException, Exception{
		List<TreeNode> trees=new ArrayList<TreeNode>();
		List list=dataBase.queryBySql("select d from Wfdept d where d.pardeptid=? and d.deletetag=0 order by d.orderid", new Object[]{new Integer(pid)});
		for(int i=0;i<list.size();i++){
			Wfdept obj=(Wfdept)list.get(i);
			Integer id=obj.getDeptid();
			int child=0;
			List clist=dataBase.queryBySql("select count(*) from Wfdept b where b.pardeptid=? and b.deletetag=0", new Object[]{id});
			child=Integer.parseInt(clist.get(0).toString());
			TreeNode node;
			if(showId)
				node=new TreeNode(id.toString(),pid.toString(),new Integer(child),"["+id+"]"+obj.getDeptname());
			else
				node=new TreeNode(id.toString(),pid.toString(),new Integer(child),obj.getDeptname());
			node.setIcon(SystemConfig.getInstance().getContextPath()+"/platform/images/dept.png");
			node.setLazy(true);
			trees.add(node);
		}
		return trees;
	}
	private void getAllChildren(TreeNode node, Wfdept dept) {
		Set children = dept.getChildren();
		if (children != null) {
			List list = node.getChildList();
			if (list == null)
				list = new ArrayList();
			Iterator it = children.iterator();
			while (it.hasNext()) {
				Wfdept dept2 = (Wfdept) it.next();
				TreeNode node2 = new TreeNode(dept2.getDeptid().toString(),
						dept2.getPardeptid() == null ? "" : dept2.getPardeptid()
								.toString(), dept2.getDeptname());
				node2.setOrderId(dept2.getOrderid());
				if(selectDept){
					for(int i=0;i<deptId.length;i++){
						if(deptId[i].equals(dept2.getDeptid().toString())){
							node2.setChecked(true);
							break;
						}
					}
				}
				node2.setExpand(true);
				node2.setIcon(SystemConfig.getInstance().getContextPath()+"/platform/images/dept.png");
				list.add(node2);
				getAllChildren(node2, dept2);
			}
			node.setChildList(list);
		}
	}
	private void getSelectDepts(){
		if(getUserId()!=null && !"".equals(getUserId())){
			try {
				List list=session.createQuery("select d from Wfuserdept d where d.id.userid=?").setString(0, getUserId()).list();
				if(list.size()>0){
					deptId=new String[list.size()];
					for(int i=0;i<list.size();i++){
						Wfuserdept udept=(Wfuserdept)list.get(i);
						deptId[i]=udept.getId().getDeptid();
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public String[] getDeptId() {
		return deptId;
	}

	public void setDeptId(String[] deptId) {
		this.deptId = deptId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public boolean isSelectDept() {
		return selectDept;
	}

	public void setSelectDept(boolean selectDept) {
		this.selectDept = selectDept;
	}

	public boolean isShowId() {
		return showId;
	}

	public void setShowId(boolean showId) {
		this.showId = showId;
	}

	public String getExpand() {
		return expand;
	}

	public void setExpand(String expand) {
		this.expand = expand;
	}

}
